<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>10-运算符</title>
  
    
      <meta 
        property="og:title" 
        content="10-运算符">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/10-%E8%BF%90%E7%AE%97%E7%AC%A6/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2004-03-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="JS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E5%AE%9A%E4%B9%89%E5%92%8C%E5%88%86%E7%B1%BB"><span class="toc-text">运算符的定义和分类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-text">运算符的定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E5%88%86%E7%B1%BB"><span class="toc-text">运算符的分类</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%97%E6%95%B0%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">算数运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%97%E6%95%B0%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E8%BF%90%E7%AE%97%E8%A7%84%E5%88%99"><span class="toc-text">算数运算符的运算规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%96%E6%A8%A1%EF%BC%88%E5%8F%96%E4%BD%99%EF%BC%89%E8%BF%90%E7%AE%97"><span class="toc-text">取模（取余）运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%AE%E7%82%B9%E6%95%B0%E8%BF%90%E7%AE%97%E7%9A%84%E7%B2%BE%E5%BA%A6%E9%97%AE%E9%A2%98"><span class="toc-text">浮点数运算的精度问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E2%80%94%E2%80%94%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E5%8A%A0%E5%8F%B7"><span class="toc-text">隐式类型转换——运算符：加号 +</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E2%80%94%E2%80%94%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A-%E3%80%81-%E3%80%81-%E3%80%81"><span class="toc-text">隐式类型转换——运算符：-、*、&#x2F;、%</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%A2%9E%E5%92%8C%E8%87%AA%E5%87%8F%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">自增和自减运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%A2%9E%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">自增运算符 ++</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%87%8F%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">自减运算符 --</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E4%B8%BE%E4%BE%8B"><span class="toc-text">代码举例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">隐式类型转换</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">一元运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#typeof"><span class="toc-text">typeof</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AD%A3%E5%8F%B7-x2F-%E8%B4%9F%E5%8F%B7%EF%BC%9A-a%E3%80%81-a"><span class="toc-text">正号&#x2F;负号：+a、-a</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E2%80%94%E2%80%94%E6%AD%A3%E5%8F%B7-x2F-%E8%B4%9F%E5%8F%B7"><span class="toc-text">隐式类型转换——正号&#x2F;负号</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E7%9B%AE%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">三目运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">逻辑运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E5%B8%83%E5%B0%94%E5%80%BC%E7%9A%84%E4%B8%8E%E6%88%96%E8%BF%90%E7%AE%97%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">非布尔值的与或运算【重要】</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E5%B8%83%E5%B0%94%E5%80%BC%E7%9A%84-%E8%BF%90%E7%AE%97"><span class="toc-text">非布尔值的 ! 运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9F%AD%E8%B7%AF%E8%BF%90%E7%AE%97%E7%9A%84%E5%A6%99%E7%94%A8%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">短路运算的妙用【重要】</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">赋值运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6%E5%8C%85%E6%8B%AC%E5%93%AA%E4%BA%9B"><span class="toc-text">赋值运算符包括哪些</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-text">注意事项</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">比较运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E6%95%B0%E5%80%BC%E7%9A%84%E6%AF%94%E8%BE%83"><span class="toc-text">非数值的比较</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AC%A6%E5%8F%B7%E7%9A%84%E5%BC%BA%E8%B0%83"><span class="toc-text">&#x3D;&#x3D;符号的强调</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%A8%E7%AD%89%E7%AC%A6%E5%8F%B7%E7%9A%84%E5%BC%BA%E8%B0%83"><span class="toc-text">&#x3D;&#x3D;&#x3D;全等符号的强调</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8D%E5%90%8C%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E4%B9%8B%E9%97%B4%E7%9A%84%E5%A4%A7%E5%B0%8F%E6%AF%94%E8%BE%83"><span class="toc-text">不同数据类型之间的大小比较</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E5%80%BC%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%85%B6%E4%BB%96%E7%B1%BB%E5%9E%8B%E6%AF%94%E8%BE%83"><span class="toc-text">数值类型和其他类型比较</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E5%A4%A7%E5%B0%8F%E6%AF%94%E8%BE%83"><span class="toc-text">日期大小比较</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%97%E5%8F%B7%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">逗号运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E4%BC%98%E5%85%88%E7%BA%A7"><span class="toc-text">运算符的优先级</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Unicode-%E7%BC%96%E7%A0%81"><span class="toc-text">Unicode 编码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      10-运算符
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2004-02-29T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2004-03-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/JS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          JS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>5.8k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/JS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            JS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<p>我们在前面讲过变量，本文讲一下<strong>运算符</strong>和表达式。</p>
<h2 id="运算符的定义和分类"><a href="#运算符的定义和分类" class="headerlink" title="运算符的定义和分类"></a>运算符的定义和分类</h2><h3 id="运算符的定义"><a href="#运算符的定义" class="headerlink" title="运算符的定义"></a>运算符的定义</h3><p><strong>运算符</strong>：也叫操作符，是一种符号。通过运算符可以对一个或多个值进行运算，并获取运算结果。</p>
<p><strong>表达式</strong>：数字、运算符、变量的组合（组成的式子）。</p>
<p>表达式最终都会有一个运算结果，我们将这个结果称为表达式的<strong>返回值</strong>。</p>
<p>比如：<code>+</code>、<code>*</code>、<code>/</code>、<code>()</code> 都是<strong>运算符</strong>，而<code>（3+5）/2</code>则是<strong>表达式</strong>。</p>
<p>比如：typeof 就是运算符，可以获得一个值的类型。它会将该值的类型以<strong>字符串</strong>的形式返回，返回值可以是 number、string、boolean、undefined、object。</p>
<h3 id="运算符的分类"><a href="#运算符的分类" class="headerlink" title="运算符的分类"></a>运算符的分类</h3><p>JS 中的运算符，分类如下：</p>
<ul>
<li>算数运算符</li>
<li>自增&#x2F;自减运算符</li>
<li>一元运算符</li>
<li>三元运算符（条件运算符）</li>
<li>逻辑运算符</li>
<li>赋值运算符</li>
<li>比较运算符</li>
</ul>
<p>下面来逐一讲解。</p>
<h2 id="算数运算符"><a href="#算数运算符" class="headerlink" title="算数运算符"></a>算数运算符</h2><p><strong>算术运算符</strong>：用于执行两个变量或值的算术运算。</p>
<p>常见的算数运算符有以下几种：</p>
<table>
<thead>
<tr>
<th align="left">运算符</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">+</td>
<td align="center">加、字符串连接</td>
</tr>
<tr>
<td align="left">-</td>
<td align="center">减</td>
</tr>
<tr>
<td align="left">*</td>
<td align="center">乘</td>
</tr>
<tr>
<td align="left">&#x2F;</td>
<td align="center">除</td>
</tr>
<tr>
<td align="left">%</td>
<td align="center">获取余数（取余、取模）</td>
</tr>
</tbody></table>
<p><strong>求余的举例</strong>：</p>
<p>假设用户输入 345，怎么分别得到 3、4、5 这三个数呢？</p>
<p><strong>答案</strong>：</p>
<pre class="highlight"><span class="line">得到3的方法：345 除以100，得到3.45然后取整，得到3。即：parseInt(345/100)</span><br><span class="line"></span><br><span class="line">得到4的方法：345 除以100，余数是45，除以10，得到4.5，取整。即：parseInt(345 % 100 / 10)</span><br><span class="line"></span><br><span class="line">得到5的方法：345 除以10，余数就是5。即：345 % 10</span><br></pre>

<h3 id="算数运算符的运算规则"><a href="#算数运算符的运算规则" class="headerlink" title="算数运算符的运算规则"></a>算数运算符的运算规则</h3><p>（1）<code>* / %</code> 的优先级高于 <code>+ -</code></p>
<p>（2）无论是<code>+ - * / %</code>都是左结合性（从左至右计算）</p>
<p>（2）小括号<code>( )</code>：能够影响计算顺序，且可以嵌套。没有中括号、没有大括号，只有小括号。</p>
<p>举例 1：（取余）</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">3</span> % <span class="number">5</span>);</span><br></pre>

<p>输出结果为 3。</p>
<p>举例 2：（注意运算符的优先级）</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="number">1</span> + ((<span class="number">2</span> * <span class="number">3</span>) % <span class="number">4</span>) / <span class="number">3</span>;</span><br></pre>

<p>结果分析：</p>
<p>原式 &#x3D; 1 + 6 % 4 &#x2F; 3 &#x3D; 1 + 2 &#x2F; 3 &#x3D; 1.66666666666666</p>
<h3 id="取模（取余）运算"><a href="#取模（取余）运算" class="headerlink" title="取模（取余）运算"></a>取模（取余）运算</h3><p>格式：</p>
<pre class="highlight"><span class="line">余数 = m % n;</span><br></pre>

<p>计算结果注意：</p>
<ul>
<li><p>取余运算结果的正负性，取决于 m，而不是 n。比如：<code>10 % -3</code>的运算结果是 1。<code>-10 % 3</code>的运算结果是-1。</p>
</li>
<li><p>如果 n &lt; 0，那就先把 n 取绝对值后，再计算。等价于 m % (-n)。</p>
</li>
<li><p>如果 n 是 0，那么结果是 NaN。</p>
</li>
<li><p>在 n &gt; 0 的情况下：</p>
<ul>
<li>如果 m&gt;&#x3D;n，那就正常取余。</li>
<li>如果 m&lt;n，那结果就是 m。</li>
</ul>
</li>
</ul>
<h3 id="浮点数运算的精度问题"><a href="#浮点数运算的精度问题" class="headerlink" title="浮点数运算的精度问题"></a>浮点数运算的精度问题</h3><p>浮点数值的最高精度是 17 位小数，但在进行算术计算时，会丢失精度，导致计算不够准确。比如：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">0.1</span> + <span class="number">0.2</span>); <span class="comment">// 运算结果不是 0.3，而是 0.30000000000000004</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">0.07</span> * <span class="number">100</span>); <span class="comment">// 运算结果不是 7，而是 7.000000000000001</span></span><br></pre>

<p>因此，<strong>不要直接判断两个浮点数是否相等</strong>。前面的文章《JavaScript 基础：基本数据类型：Number》有详细介绍。</p>
<h3 id="隐式类型转换——运算符：加号"><a href="#隐式类型转换——运算符：加号" class="headerlink" title="隐式类型转换——运算符：加号 +"></a>隐式类型转换——运算符：加号 <code>+</code></h3><ol>
<li><strong>字符串 + XX &#x3D; 字符串</strong></li>
</ol>
<p>任何值和字符串做加法运算，都会先调用 String() 函数转换为字符串，然后再做拼串操作。最终的运算结果是字符串。</p>
<p>比如：</p>
<pre class="highlight"><span class="line">result1 = <span class="number">1</span> + <span class="number">2</span> + <span class="string">&#x27;3&#x27;</span>; <span class="comment">// 字符串：33</span></span><br><span class="line"></span><br><span class="line">result2 = <span class="string">&#x27;1&#x27;</span> + <span class="number">2</span> + <span class="number">3</span>; <span class="comment">// 字符串：123</span></span><br></pre>

<ol start="2">
<li><strong>Boolean + 数字 &#x3D; 数字</strong></li>
</ol>
<p>Boolean 型和数字型相加时， true 按 1 来算 ，false 按 0 来算。这里其实是先调 Number() 函数，将 Boolean 类型转为 Number 类型，然后再和 数字相加。</p>
<ol start="3">
<li><strong>null + 数字 &#x3D; 数字</strong></li>
</ol>
<p>等价于：0 + 数字</p>
<ol start="4">
<li><strong>undefined + 数字 &#x3D; NaN</strong></li>
</ol>
<p>计算结果：NaN</p>
<ol start="5">
<li>任何值和 <strong>NaN</strong> 运算的结果都是 NaN。</li>
</ol>
<h3 id="隐式类型转换——运算符：-、-、-、"><a href="#隐式类型转换——运算符：-、-、-、" class="headerlink" title="隐式类型转换——运算符：-、*、/、%"></a>隐式类型转换——运算符：<code>-</code>、<code>*</code>、<code>/</code>、<code>%</code></h3><p>任何非 Number 类型的值做<code>-</code>、<code>*</code>、<code>/</code>、<code>%</code>运算时，会将这些值转换为 Number 然后再运算(内部调用的是 Number() 函数），运算结果是 Number 类型。</p>
<p>任何数据和 NaN进行运算，结果都是NaN。</p>
<p>比如：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> result1 = <span class="number">100</span> - <span class="string">&#x27;1&#x27;</span>; <span class="comment">// 99</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> result2 = <span class="literal">true</span> + <span class="title class_">NaN</span>; <span class="comment">// NaN</span></span><br></pre>

<h2 id="自增和自减运算符"><a href="#自增和自减运算符" class="headerlink" title="自增和自减运算符"></a>自增和自减运算符</h2><h3 id="自增运算符"><a href="#自增运算符" class="headerlink" title="自增运算符 ++"></a>自增运算符 <code>++</code></h3><p>作用：可以快速对一个变量进行加1操作。只能操作变量，不能操作常量或者表达式。</p>
<p>例如：</p>
<pre class="highlight"><span class="line"><span class="keyword">let</span> a1 = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">let</span> a2 = <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">a1++;</span><br><span class="line"><span class="keyword">const</span> result = a1++ + a2; <span class="comment">// result的结果为4</span></span><br><span class="line"><span class="comment">// (a1+a2)++; // 报错，没有这种写法</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> a3 = <span class="number">3</span>;</span><br><span class="line">a3++; <span class="comment">// 报错，因为常量无法再自加</span></span><br></pre>

<p>自增分成两种：<code>a++</code>和<code>++a</code>。共同点：</p>
<p>（1）无论是 <code>a++</code> 还是<code>++a</code>，自增都会使原变量的值加 1。</p>
<p>（2）<strong>我们要注意的是</strong>：<code>a</code>是变量，而<code>a++</code>和<code>++a</code>是<strong>表达式</strong>。</p>
<p>那这两种自增，有啥区别呢？区别是：<code>a++</code> 和 <code>++a</code>的值不同：（也就是说，表达式的值不同）</p>
<ul>
<li><p><code>a++</code>这个表达式的值等于原变量的值（a 自增前的值）。可以这样理解：先把 a 的值赋值给表达式，然后 a 再自增。</p>
</li>
<li><p><code>++a</code>这个表达式的值等于新值 （a 自增后的值）。 可以这样理解：a 先自增，然后把自增后的值赋值给表达式。</p>
</li>
</ul>
<h3 id="自减运算符"><a href="#自减运算符" class="headerlink" title="自减运算符 --"></a>自减运算符 <code>--</code></h3><p>作用：可以快速对一个变量进行减1操作。原理同自增运算符。</p>
<p>开发时，大多使用后置的自增&#x2F;自减，并且代码独占一行，例如：<code>num++</code>，或者 <code>num--</code>。</p>
<h3 id="代码举例"><a href="#代码举例" class="headerlink" title="代码举例"></a>代码举例</h3><pre class="highlight"><span class="line"><span class="keyword">var</span> n1 = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">var</span> n2 = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> result1 = n1++;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(n1); <span class="comment">// 11</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result1); <span class="comment">// 10</span></span><br><span class="line"></span><br><span class="line">result = ++n1;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(n1); <span class="comment">//12</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result); <span class="comment">//12</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> result2 = n2--;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(n2); <span class="comment">// 19</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result2); <span class="comment">// 20</span></span><br><span class="line"></span><br><span class="line">result2 = --n2;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(n2); <span class="comment">// 18</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result2); <span class="comment">// 18</span></span><br></pre>

<h3 id="隐式类型转换"><a href="#隐式类型转换" class="headerlink" title="隐式类型转换"></a>隐式类型转换</h3><p>自增和自减时，a的执行过程：</p>
<p>（1）先调用<code>Number(a)</code>函数；</p>
<p>（2）然后将<code>Number(a)</code>的返回结果进行 加 1 操作，得到的结果赋值给 a。</p>
<p>举例 1：</p>
<pre class="highlight"><span class="line"><span class="keyword">let</span> a = <span class="string">&#x27;666&#x27;</span>; <span class="comment">// 这里不能用 const 定义，否则报错。</span></span><br><span class="line">a++;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a); <span class="comment">// 打印结果：667</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a); <span class="comment">// 打印结果： number</span></span><br></pre>

<p>举例2：</p>
<pre class="highlight"><span class="line"><span class="keyword">let</span> a = <span class="string">&#x27;abc&#x27;</span>;</span><br><span class="line">a++;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a); <span class="comment">// 打印结果：NaN。因为 Number(&#x27;abc&#x27;)的结果为 NaN，再自增后，结果依然是 NaN</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a); <span class="comment">// 打印结果：number</span></span><br></pre>

<h2 id="一元运算符"><a href="#一元运算符" class="headerlink" title="一元运算符"></a>一元运算符</h2><p>一元运算符，只需要一个操作数。常见的一元运算符如下。</p>
<h3 id="typeof"><a href="#typeof" class="headerlink" title="typeof"></a>typeof</h3><blockquote>
<p>typeof 就是典型的一元运算符，因为后面只跟一个操作数。</p>
</blockquote>
<p><code>typeof()</code>表示“<strong>获取变量的数据类型</strong>”，它是 JS 提供的一个操作符。返回的是小写，语法为：（两种写法都可以）</p>
<pre class="highlight"><span class="line"><span class="comment">// 写法1</span></span><br><span class="line"><span class="keyword">typeof</span> 变量;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 写法2</span></span><br><span class="line"><span class="title function_">typeof</span>(变量);</span><br></pre>

<p>typeof 这个运算符的返回结果就是变量的类型。那返回结果的类型是什么呢？是字符串。</p>
<p><strong>返回结果</strong>：</p>
<table>
<thead>
<tr>
<th align="left">typeof 的语法</th>
<th align="center">返回结果</th>
</tr>
</thead>
<tbody><tr>
<td align="left">typeof 数字（含 typeof NaN）</td>
<td align="center">number</td>
</tr>
<tr>
<td align="left">typeof 字符串</td>
<td align="center">string</td>
</tr>
<tr>
<td align="left">typeof 布尔型</td>
<td align="center">boolean</td>
</tr>
<tr>
<td align="left">typeof 对象</td>
<td align="center">object</td>
</tr>
<tr>
<td align="left">typeof 方法</td>
<td align="center">function</td>
</tr>
<tr>
<td align="left">typeof null</td>
<td align="center">object</td>
</tr>
<tr>
<td align="left">typeof undefined</td>
<td align="center">undefined</td>
</tr>
</tbody></table>
<p>备注 1：为啥 <code>typeof null</code>的返回值也是 object 呢？因为 null 代表的是<strong>空对象</strong>。</p>
<p>备注 2：<code>typeof NaN</code>的返回值是 number，上一篇文章中讲过，NaN 是一个特殊的数字。</p>
<p><strong>返回结果举例</strong>：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;123&#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a); <span class="comment">// 打印结果：string</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> []); <span class="comment">// 空数组的打印结果：object</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> &#123;&#125;); <span class="comment">// 空对象的打印结果：object</span></span><br></pre>

<p>代码解释：这里的空数组<code>[]</code>、空对象<code>&#123;&#125;</code> ，为啥他们在使用 typeof 时，返回值也是 <code>object</code>呢？因为空数组、空对象都是<strong>引用数据类型 Object</strong>。</p>
<p>typeof 无法区分数组，但 instanceof 可以。比如：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>([] <span class="keyword">instanceof</span> <span class="title class_">Array</span>); <span class="comment">// 打印结果：true</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(&#123;&#125; <span class="keyword">instanceof</span> <span class="title class_">Array</span>); <span class="comment">// 打印结果：false</span></span><br></pre>

<p>关于 instanceof 的详细内容，以后讲对象的时候，会详细介绍。</p>
<h3 id="正号-x2F-负号：-a、-a"><a href="#正号-x2F-负号：-a、-a" class="headerlink" title="正号&#x2F;负号：+a、-a"></a>正号&#x2F;负号：<code>+a</code>、<code>-a</code></h3><blockquote>
<p>注意，这里说的是正号&#x2F;负号，不是加号&#x2F;减号。</p>
</blockquote>
<p>（1）不会改变原数值。</p>
<p>（1）正号不会对数字产生任何影响。比如说，<code>2</code>和<code>+2</code>是一样的。</p>
<p>（2）我们可以对其他的数据类型使用<code>+</code>，来将其转换为 number【重要的小技巧】。比如：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="literal">true</span>;</span><br><span class="line">a = +a; <span class="comment">// 注意这行代码的一元运算符操作</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;a：&#x27;</span> + a);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;-----------------&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> b = <span class="string">&#x27;18&#x27;</span>;</span><br><span class="line">b = +b; <span class="comment">// 注意这行代码的一元运算符操作</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;b：&#x27;</span> + b);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> b);</span><br></pre>

<p>打印结果：</p>
<pre class="highlight"><span class="line">a：1</span><br><span class="line">number</span><br><span class="line"></span><br><span class="line">-----------------</span><br><span class="line"></span><br><span class="line">b：18</span><br><span class="line">number</span><br></pre>

<p>（3）负号可以对数字进行取反。</p>
<h3 id="隐式类型转换——正号-x2F-负号"><a href="#隐式类型转换——正号-x2F-负号" class="headerlink" title="隐式类型转换——正号&#x2F;负号"></a>隐式类型转换——正号&#x2F;负号</h3><p>任何值做<code>+a</code>、<code>-a</code>运算时， 内部调用的是 Number() 函数。</p>
<p><strong>举例</strong>：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> a = <span class="string">&#x27;666&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> b = +a; <span class="comment">// 对 a 进行一元运算，b是运算结果</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a); <span class="comment">// 打印结果：string。说明 a 的数据类型保持不变。</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a); <span class="comment">// 打印结果：&quot;666&quot;。不会改变原数值。</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> b); <span class="comment">// 打印结果：number。说明 b 的数据类型发生了变化。</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(b); <span class="comment">// 打印结果：666</span></span><br></pre>

<h2 id="三目运算符"><a href="#三目运算符" class="headerlink" title="三目运算符"></a>三目运算符</h2><p>三目运算符也叫三元运算符、条件运算符。</p>
<p>语法：</p>
<pre class="highlight"><span class="line">条件表达式 ? 语句1 : 语句2;</span><br></pre>

<p><strong>执行流程</strong>——条件运算符在执行时，首先对条件表达式进行求值：</p>
<ul>
<li><p>如果该值为 true，则执行语句 1，并返回执行结果</p>
</li>
<li><p>如果该值为 false，则执行语句 2，并返回执行结果</p>
</li>
</ul>
<p>如果条件表达式的求值结果是一个非布尔值，会将其转换为布尔值然后再运算。</p>
<h2 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h2><p>逻辑运算符有三个：</p>
<ul>
<li><p><code>&amp;&amp;</code>： 与（且）。两个都为真，结果才为真。特点：一假则假。</p>
</li>
<li><p><code>||</code> ：或。只要有一个是真，结果就是真。特点：特点: 一真则真。</p>
</li>
<li><p><code>!</code> ：非。对一个布尔值进行取反。特点: 真变假, 假变真。</p>
</li>
</ul>
<p>注意：能参与逻辑运算的，都是布尔值。</p>
<p><strong>连比的写法：</strong></p>
<p>来看看逻辑运算符连比的写法。</p>
<p>举例 1：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">3</span> &lt; <span class="number">2</span> &amp;&amp; <span class="number">2</span> &lt; <span class="number">4</span>);</span><br></pre>

<p>输出结果为 false。</p>
<p>举例 2：（判断一个人的年龄是否在 18~65 岁之间）</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> a = <span class="title function_">prompt</span>(<span class="string">&#x27;请输入您的年龄&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (a &gt;= <span class="number">18</span> &amp;&amp; a &lt; <span class="number">65</span>) &#123;</span><br><span class="line">    <span class="title function_">alert</span>(<span class="string">&#x27;可以上班&#x27;</span>);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="title function_">alert</span>(<span class="string">&#x27;准备退休&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre>

<p>PS：上面的<code>a&gt;=18 &amp;&amp; a&lt;= 65</code>千万别想当然地写成<code> 18&lt;= a &lt;= 65</code>，没有这种语法。</p>
<h3 id="非布尔值的与或运算【重要】"><a href="#非布尔值的与或运算【重要】" class="headerlink" title="非布尔值的与或运算【重要】"></a>非布尔值的与或运算【重要】</h3><blockquote>
<p>之所以重要，是因为在实际开发中，我们经常用这种代码做容错处理或者兜底处理。</p>
</blockquote>
<p>非布尔值进行<strong>与或运算</strong>时，会先将其转换为布尔值，然后再运算，但返回结果是<strong>原值</strong>。比如说：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> result = <span class="number">5</span> &amp;&amp; <span class="number">6</span>; <span class="comment">// 运算过程：true &amp;&amp; true;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;result：&#x27;</span> + result); <span class="comment">// 打印结果：6（也就是最后面的那个值）</span></span><br></pre>

<p>上方代码可以看到，虽然运算过程为布尔值的运算，但返回结果是原值。</p>
<p>那么，返回结果是哪个原值呢？我们来看一下。</p>
<p>1、两个非布尔值，做逻辑运算：</p>
<p><strong>与运算</strong>的返回结果：</p>
<ul>
<li><p>如果第一个值为 false，则只执行第一条语句，并直接返回第一个值；不会再往后执行。</p>
</li>
<li><p>如果第一个值为 true，则继续执行第二条语句，并返回第二个值（无论第二个值的结果如何）。</p>
</li>
</ul>
<p><strong>或运算</strong>的返回结果：</p>
<ul>
<li><p>如果第一个值为 true，则只执行第一条语句，并直接返回第一个值；不会再往后执行。</p>
</li>
<li><p>如果第一个值为 false，则继续执行第二条语句，并返回第二个值（无论第二个值的结果如何）。</p>
</li>
</ul>
<p>2、三个及以上的非布尔值，做逻辑运算：</p>
<p><strong>与运算</strong>的返回结果：（value1 &amp;&amp; value2 &amp;&amp; value3）</p>
<ul>
<li>从左到右依次计算操作数，找到第一个为 false 的值为止。</li>
<li>如果所有的值都为 true，则返回最后一个值。</li>
</ul>
<p><strong>或运算</strong>的返回结果：（value1 || value2 || value3）</p>
<ul>
<li>从左到右依次计算操作数，找到第一个为 true 的值为止。</li>
<li>如果所有的值都为 false，则返回最后一个值。</li>
</ul>
<h3 id="非布尔值的-运算"><a href="#非布尔值的-运算" class="headerlink" title="非布尔值的 ! 运算"></a>非布尔值的 <code>!</code> 运算</h3><p>非布尔值进行<strong>非运算</strong>时，会先将其转换为布尔值，然后再运算，返回结果是<strong>布尔值</strong>。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">let</span> a = <span class="number">10</span>;</span><br><span class="line">a = !a;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a); <span class="comment">// false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a); <span class="comment">// boolean</span></span><br></pre>

<h3 id="短路运算的妙用【重要】"><a href="#短路运算的妙用【重要】" class="headerlink" title="短路运算的妙用【重要】"></a>短路运算的妙用【重要】</h3><blockquote>
<p>下方举例中的写法技巧，在实际开发中，经常用到。这种写法，是一种很好的「容错、容灾、降级」方案，需要多看几遍。</p>
</blockquote>
<p>1、JS 中的<code>&amp;&amp;</code>属于<strong>短路</strong>的与：</p>
<ul>
<li><p>如果第一个值为 false，则不会执行后面的内容。</p>
</li>
<li><p>如果第一个值为 true，则继续执行第二条语句，并返回第二个值。</p>
</li>
</ul>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> a1 = <span class="string">&#x27;qianguyihao&#x27;</span>;</span><br><span class="line"><span class="comment">// 第一个值为true，会继续执行后面的内容</span></span><br><span class="line">a1 &amp;&amp; <span class="title function_">alert</span>(<span class="string">&#x27;看 a1 出不出来&#x27;</span>); <span class="comment">// 可以弹出 alert 框</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> a2 = <span class="literal">undefined</span>;</span><br><span class="line"><span class="comment">// 第一个值为false，不会继续执行后面的内容</span></span><br><span class="line">a2 &amp;&amp; <span class="title function_">alert</span>(<span class="string">&#x27;看 a2 出不出来&#x27;</span>); <span class="comment">// 不会弹出 alert 框</span></span><br></pre>

<p>2、JS 中的<code>||</code>属于<strong>短路</strong>的或：</p>
<ul>
<li><p>如果第一个值为 true，则不会执行后面的内容。</p>
</li>
<li><p>如果第一个值为 false，则继续执行第二条语句，并返回第二个值。</p>
</li>
</ul>
<p>实际开发中，我们经常是这样来做「容错处理」的，如下。</p>
<p>举例1：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> result; <span class="comment">// 请求接口时，后台返回的内容</span></span><br><span class="line"><span class="keyword">let</span> errorMsg = <span class="string">&#x27;&#x27;</span>; <span class="comment">// 前端的文案提示</span></span><br><span class="line"><span class="keyword">if</span> (result &amp; result.<span class="property">retCode</span> == <span class="number">0</span>) &#123;</span><br><span class="line">  errorMsg = <span class="string">&#x27;恭喜你中奖啦~&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (result &amp;&amp; result.<span class="property">retCode</span> != <span class="number">0</span>) &#123;</span><br><span class="line">	<span class="comment">// 接口返回异常码时</span></span><br><span class="line">	errorMsg = result.<span class="property">msg</span> || <span class="string">&#x27;活动太火爆，请稍后再试&#x27;</span>; <span class="comment">// 文案提示信息，优先用 接口返回的msg字段，其次用 &#x27;活动太火爆，请稍后再试&#x27; 这个文案兜底。</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (!result) &#123;</span><br><span class="line">	<span class="comment">// 接口挂掉时</span></span><br><span class="line">	errorMsg = <span class="string">&#x27;网络异常，请稍后再试&#x27;</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>举例2，当前端成功调用一个接口后，返回的数据为 result 对象。这个时候，我们用变量 a 来接收 result 里的图片资源：</p>
<pre class="highlight"><span class="line"><span class="keyword">if</span> (result.<span class="property">retCode</span> == <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> a = result &amp;&amp; result.<span class="property">data</span> &amp;&amp; result.<span class="property">data</span>.<span class="property">imgUrl</span> || <span class="string">&#x27;http://img.smyhvae.com/20160401_01.jpg&#x27;</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>上方代码的意思是，获取返回结果中的<code>result.data.imgUrl</code>这个图片资源；如果返回结果中没有 <code>result.data.imgUrl</code> 这个字段，就用 <code>http://img.smyhvae.com/20160401_01.jpg</code> 作为<strong>兜底</strong>图片。这种写法，在实际开发中经常用到。</p>
<h2 id="赋值运算符"><a href="#赋值运算符" class="headerlink" title="赋值运算符"></a>赋值运算符</h2><p>赋值：将等号右侧的值赋给符号左侧的变量。</p>
<h3 id="赋值运算符包括哪些"><a href="#赋值运算符包括哪些" class="headerlink" title="赋值运算符包括哪些"></a>赋值运算符包括哪些</h3><ul>
<li><p><code>=</code> 直接赋值。比如 <code>var a = 5</code>。意思是，把 5 这个值，往 a 里面存一份。简称：把 5 赋值给 a。</p>
</li>
<li><p><code>+=</code>：比如 a +&#x3D; 5 等价于 a &#x3D; a + 5。</p>
</li>
<li><p><code>-=</code>：比如 a -&#x3D; 5 等价于 a &#x3D; a - 5。</p>
</li>
<li><p><code>*=</code>：比如 a _ &#x3D; 5 等价于 a &#x3D; a -5。</p>
</li>
<li><p><code>/=</code>：比如 a &#x2F;&#x3D; 5 等价于 a &#x3D; a &#x2F; 5。</p>
</li>
<li><p><code>%=</code>：比如 a %&#x3D; 5 等价于 a &#x3D; a % 5。</p>
</li>
</ul>
<h3 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项"></a>注意事项</h3><p>（1）算数运算符的优先级高于赋值运算符。举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> result = <span class="number">1</span> + <span class="number">2</span>; <span class="comment">// 先计算 1 + 2，再把计算结果赋值给 result。因为算数运算符的优先级高于赋值运算符。</span></span><br></pre>

<p>（2）赋值运算符的结合性是右结合性（从右至左的顺序计算）。举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> a1, a2;</span><br><span class="line">a1 = a2 = <span class="number">3</span>; <span class="comment">// 先将 3 复制给 a2，再将 a2 的值赋值给 a1</span></span><br></pre>







<h2 id="比较运算符"><a href="#比较运算符" class="headerlink" title="比较运算符"></a>比较运算符</h2><p>比较运算符可以比较两个值之间的大小关系，如果关系成立它会返回 true，如果关系不成立则返回 false。</p>
<p>比较运算符有很多种，比如：</p>
<pre class="highlight"><span class="line">&gt;	大于号</span><br><span class="line">&lt;	小于号</span><br><span class="line">&gt;= 	大于或等于</span><br><span class="line">&lt;=  小于或等于</span><br><span class="line">== 	等于</span><br><span class="line">=== 全等于</span><br><span class="line">!=	不等于</span><br><span class="line">!== 不全等于</span><br></pre>

<p><strong>比较运算符，得到的结果都是布尔值：要么是 true，要么是 false</strong>。如果关系成立，就返回true；如果关系不成立，就返回false。</p>
<p>举例如下：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> result = <span class="number">5</span> &gt; <span class="number">10</span>; <span class="comment">// false</span></span><br></pre>

<h3 id="非数值的比较"><a href="#非数值的比较" class="headerlink" title="非数值的比较"></a>非数值的比较</h3><p>（1）对于非数值进行比较时，会将其转换为数值类型（内部是调用<code>Number()方法</code>），再进行比较。</p>
<p>举例如下：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">1</span> &gt; <span class="literal">true</span>); <span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">1</span> &gt;= <span class="literal">true</span>); <span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">1</span> &gt; <span class="string">&#x27;0&#x27;</span>); <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//console.log(10 &gt; null); //true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//任何值和NaN做任何比较都是false</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">10</span> &lt;= <span class="string">&#x27;hello&#x27;</span>); <span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">true</span> &gt; <span class="literal">false</span>); <span class="comment">//true</span></span><br></pre>

<p>（2）特殊情况：如果参与比较的都是字符串，则<strong>不会</strong>将其转换为数字进行比较，比较的是字符串的<strong>Unicode 编码</strong>。【非常重要，这里是个大坑，很容易踩到】</p>
<p>比较字符编码时，是一位一位进行比较，顺序从左到右。如果大一样，则继续比较下一位。</p>
<p>比如说，当你尝试去比较<code>&quot;123&quot;</code>和<code>&quot;56&quot;</code>这两个字符串时，你会发现，字符串”56”竟然比字符串”123”要大（因为 5 比 1 大）。也就是说，下面这样代码的打印结果，其实是 true:（这个我们一定要注意，在日常开发中，很容易忽视）</p>
<pre class="highlight"><span class="line"><span class="comment">// 比较两个字符串时，比较的是字符串的字符编码，所以可能会得到不可预期的结果</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;56&#x27;</span> &gt; <span class="string">&#x27;123&#x27;</span>); <span class="comment">// true</span></span><br></pre>

<p><strong>因此</strong>：当我们想比较两个字符串型的数字时，<strong>一定一定要先转型</strong>再比较大小，比如 <code>parseInt()</code>。</p>
<p>（3）任何值和 NaN 做任何比较都是 false。</p>
<h3 id="符号的强调"><a href="#符号的强调" class="headerlink" title="==符号的强调"></a><code>==</code>符号的强调</h3><p><code>==</code>这个符号，它是<strong>判断是否等于</strong>，而不是赋值。注意事项如下：</p>
<p>（1）<code>== </code>这个符号，还可以验证字符串是否相同。例如：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;我爱你中国&#x27;</span> == <span class="string">&#x27;我爱你中国&#x27;</span>); <span class="comment">// 输出结果为true</span></span><br></pre>

<p>（2）<code>== </code>这个符号并不严谨，会做隐式转换，将不同的数据类型，<strong>转为相同类型</strong>进行比较。例如：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;6&#x27;</span> == <span class="number">6</span>); <span class="comment">// 打印结果：true。这里的字符串&quot;6&quot;会先转换为数字6，然后再进行比较</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">true</span> == <span class="string">&#x27;1&#x27;</span>); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">0</span> == -<span class="number">0</span>); <span class="comment">// 打印结果：true</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">null</span> == <span class="number">0</span>); <span class="comment">// 打印结果：false</span></span><br></pre>

<p>（3）undefined 衍生自 null，所以这两个值做相等判断时，会返回 true。</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">undefined</span> == <span class="literal">null</span>); <span class="comment">//打印结果：true。</span></span><br></pre>

<p>（4）NaN 不和任何值相等，包括它本身。</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">NaN</span> == <span class="title class_">NaN</span>); <span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">NaN</span> === <span class="title class_">NaN</span>); <span class="comment">//false</span></span><br></pre>

<p>问题：那如果我想判断 b 的值是否为 NaN，该怎么办呢？</p>
<p>答案：可以通过 isNaN()函数来判断一个值是否是 NaN。举例：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">isNaN</span>(b));</span><br></pre>

<p>如上方代码所示，如果 b 为 NaN，则返回 true；否则返回 false。</p>
<h3 id="全等符号的强调"><a href="#全等符号的强调" class="headerlink" title="===全等符号的强调"></a><code>===</code>全等符号的强调</h3><p><strong>全等在比较时，不会做类型转换</strong>。如果要保证<strong>完全等于</strong>（即：不仅要判断取值相等，还要判断数据类型相同），我们就要用三个等号<code>===</code>。例如：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;6&#x27;</span> === <span class="number">6</span>); <span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">6</span> === <span class="number">6</span>); <span class="comment">//true</span></span><br></pre>

<p>上述内容分析出：</p>
<ul>
<li><p><code>==</code>两个等号，不严谨，”6”和 6 是 true。</p>
</li>
<li><p><code>===</code>三个等号，严谨，”6”和 6 是 false。</p>
</li>
</ul>
<p>另外还有：**<code>==</code>的反面是<code>!=</code>，<code>===</code>的反面是<code>!==</code>**。例如：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">3</span> != <span class="number">8</span>); <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">3</span> != <span class="string">&#x27;3&#x27;</span>); <span class="comment">// false，因为3==&quot;3&quot;是true，所以反过来就是false。</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">3</span> !== <span class="string">&#x27;3&#x27;</span>); <span class="comment">// true，应为3===&quot;3&quot;是false，所以反过来是true。</span></span><br></pre>

<h2 id="不同数据类型之间的大小比较"><a href="#不同数据类型之间的大小比较" class="headerlink" title="不同数据类型之间的大小比较"></a>不同数据类型之间的大小比较</h2><p>这一段是比较运算符的延伸，内容繁琐，新手可以不用记，等以后用到的时候再查阅。</p>
<h3 id="数值类型和其他类型比较"><a href="#数值类型和其他类型比较" class="headerlink" title="数值类型和其他类型比较"></a>数值类型和其他类型比较</h3><p>先将其他类型隐式转换为数值类型（内部是调用<code>Number()</code>方法），然后比较大小。代码举例：</p>
<pre class="highlight"><span class="line"><span class="comment">//字符串与数字比较</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;200&#x27;</span> &gt; <span class="number">100</span>); <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;a&#x27;</span> &gt; <span class="number">100</span>); <span class="comment">// false。 &#x27;a&#x27; 被转换成 NaN 进行比较</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;110a&#x27;</span> &gt; <span class="number">100</span>); <span class="comment">// false。 &#x27;110a&#x27; 被转换成 NaN 进行比较。说明`110a`在做隐式转换的时候，是调用了 Number(&#x27;110a&#x27;)方法，而不是调用  parseInt(&#x27;110a&#x27;)方法</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 布尔值与数字比较</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">true</span> == <span class="number">1</span>); <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">false</span> == <span class="number">0</span>); <span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// null 与数字进行比较</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">null</span> &lt; <span class="number">0</span>); <span class="comment">// false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">null</span> == <span class="number">0</span>); <span class="comment">// false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">null</span> &gt; <span class="number">0</span>); <span class="comment">// false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">null</span> &lt;= <span class="number">0</span>); <span class="comment">// true。这是一个很严重的bug</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">null</span> &gt;= <span class="number">0</span>); <span class="comment">// true。同上</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// undefined 与数字进行比较：结果都是 false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">undefined</span> &gt; <span class="number">0</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">undefined</span> == <span class="number">0</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">undefined</span> &lt; <span class="number">0</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">undefined</span> &gt;= <span class="number">0</span>);</span><br></pre>

<h3 id="日期大小比较"><a href="#日期大小比较" class="headerlink" title="日期大小比较"></a>日期大小比较</h3><p>如果日期的格式为字符串，则比较字符串的<strong>Unicode 编码</strong>。代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> myDate1 = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="number">2022</span>, <span class="number">8</span>, <span class="number">8</span>);</span><br><span class="line"><span class="keyword">const</span> myDate2 = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="number">2022</span>, <span class="number">8</span>, <span class="number">9</span>);</span><br><span class="line"><span class="keyword">const</span> myDate3 = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="number">2022</span>, <span class="number">9</span>, <span class="number">8</span>);</span><br><span class="line"><span class="keyword">const</span> myDate4 = <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="number">2023</span>, <span class="number">8</span>, <span class="number">8</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate1 &lt; myDate2); <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate1 &lt; myDate3); <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate3 &lt; myDate4); <span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> date1 = <span class="string">&#x27;2022-08-08&#x27;</span>; <span class="comment">// &quot;2022/08/08&quot;同理</span></span><br><span class="line"><span class="keyword">const</span> date2 = <span class="string">&#x27;2022-08-09&#x27;</span>; <span class="comment">// &quot;2022/08/09&quot;同理</span></span><br><span class="line"><span class="keyword">const</span> date3 = <span class="string">&#x27;2022-09-08&#x27;</span>; <span class="comment">// &quot;2022/09/08&quot;同理</span></span><br><span class="line"><span class="keyword">const</span> date4 = <span class="string">&#x27;2023-08-08&#x27;</span>; <span class="comment">// &quot;2023/08/08&quot;同理</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date1 &lt; date2); <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date1 &lt; date3); <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date3 &lt; date4); <span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> time1 = <span class="string">&#x27;2022-08-08 08:00:00&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> time2 = <span class="string">&#x27;2022-08-08 08:00:01&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> time3 = <span class="string">&#x27;2022-08-08 08:01:00&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> time4 = <span class="string">&#x27;2022-08-08 09:00:00&#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(time1 &lt; time2); <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(time1 &lt; time3); <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(time1 &lt; time4); <span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 数据类型不同，此处是先将 myDate1 转为字符串类型，然后比较大小。可想而知，结果都是 false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate1 &gt;= date1); <span class="comment">// false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(myDate1 &lt;= date1); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 虽然时间格式不同，但都是字符串，所以可以比较大小</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(date1 &lt; time1); <span class="comment">// true</span></span><br></pre>

<p>参考链接：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/w390058785/article/details/79957206">【JavaScript】探究数据类型之间的隐式转换和大小比较</a></li>
</ul>
<h2 id="逗号运算符"><a href="#逗号运算符" class="headerlink" title="逗号运算符"></a>逗号运算符</h2><p>逗号运算符一般用于简化代码。逗号运算符的优先级是所有运算符中最低的。</p>
<p>逗号运算符也是一个运算符, 所以也有运算符结果。它的运算符结果是最后一个表达式的结果。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="comment">// 利用逗号运算符同时定义多个变量</span></span><br><span class="line"><span class="keyword">let</span> a, b;</span><br><span class="line"><span class="comment">// 利用逗号运算符同时给多个变量赋值</span></span><br><span class="line">a = <span class="number">10</span>, b = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> res1 = (<span class="number">1</span> + <span class="number">2</span>, <span class="number">3</span> + <span class="number">4</span>, <span class="number">5</span> + <span class="number">6</span>); <span class="comment">// 打印结果：11</span></span><br></pre>

<h2 id="运算符的优先级"><a href="#运算符的优先级" class="headerlink" title="运算符的优先级"></a>运算符的优先级</h2><p>运算符的优先级如下：（优先级从高到低）</p>
<ul>
<li><p><code>.</code>、<code>[]</code>、<code>new</code></p>
</li>
<li><p><code>()</code></p>
</li>
<li><p><code>++</code>、<code>--</code></p>
</li>
<li><p><code>!</code>、<code>~</code>、<code>+</code>（单目）、<code>-</code>（单目）、<code>typeof</code>、<code>void</code>、<code>delete</code></p>
</li>
<li><p><code>*</code>、<code>/</code>、<code>%</code></p>
</li>
<li><p><code>+</code>（双目）、<code>-</code>（双目）</p>
</li>
<li><p><code>&lt;&lt;</code>、<code>&gt;&gt;</code>、<code>&gt;&gt;&gt;</code></p>
</li>
<li><p>比较运算符：<code>&lt;</code>、<code>&lt;=</code>、<code>&gt;</code>、<code>&gt;=</code></p>
</li>
<li><p>比较运算符：<code>==</code>、<code>!==</code>、<code>===</code>、<code>!==</code></p>
</li>
<li><p><code>&amp;</code></p>
</li>
<li><p><code>^</code></p>
</li>
<li><p><code>|</code></p>
</li>
<li><p>逻辑运算符：<code>&amp;&amp;</code> （注意：逻辑与 <code>&amp;&amp;</code> 比逻辑或 <code>||</code> 的优先级更高）</p>
</li>
<li><p>逻辑运算符：<code>||</code></p>
</li>
<li><p><code>?:</code></p>
</li>
<li><p><code>=</code>、<code>+=</code>、<code>-=</code>、<code>*=</code>、<code>/=</code>、<code>%=</code>、<code>&lt;&lt;=</code>、<code>&gt;&gt;=</code>、<code>&gt;&gt;&gt;=</code>、<code>&amp;=</code>、<code>^=</code>、<code>|=</code></p>
</li>
<li><p><code>,</code></p>
</li>
</ul>
<p>备注：在实际写代码的时候，如果你不清楚哪个优先级更高，可以先尝试把括号用上。</p>
<h2 id="Unicode-编码"><a href="#Unicode-编码" class="headerlink" title="Unicode 编码"></a>Unicode 编码</h2><blockquote>
<p>这一段中，我们来讲引申的内容：Unicode 编码的使用。</p>
</blockquote>
<p>各位同学可以先在网上查一下“Unicode 编码表”。</p>
<p>1、在字符串中可以使用转义字符输入 Unicode 编码。格式如下：</p>
<pre class="highlight"><span class="line">\u四位编码</span><br></pre>

<p>举例如下：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;\u2600&#x27;</span>); <span class="comment">// 这里的 2600 采用的是16进制</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;\u2602&#x27;</span>); <span class="comment">// 这里的 2602 采用的是16进制。</span></span><br></pre>

<p>打印结果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20181222_1218.png" class="lozad post-image"src="http://img.smyhvae.com/20181222_1218.png"></p>
<p>2、我们还可以在 HTML 网页中使用 Unicode 编码。格式如下：</p>
<pre class="highlight"><span class="line">&amp;#四位编码;</span><br></pre>

<p>PS：我们知道，Unicode 编码采用的是 16 进制，但是，这里的编码需要使用 10 进制。</p>
<p>举例如下：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">h1</span> <span class="attr">style</span>=<span class="string">&quot;font-size: 100px;&quot;</span>&gt;</span><span class="symbol">&amp;#9860;</span><span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br></pre>

<p>打印结果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20181222_1226.png" class="lozad post-image"src="http://img.smyhvae.com/20181222_1226.png"></p>
<h2 id="赞赏作者"><a href="#赞赏作者" class="headerlink" title="赞赏作者"></a>赞赏作者</h2><p>创作不易，你的赞赏和认可，是我更新的最大动力：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://img.smyhvae.com/20220401_1800.jpg" class="lozad post-image"src="https://img.smyhvae.com/20220401_1800.jpg"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/10-%E8%BF%90%E7%AE%97%E7%AC%A6/">
            https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/10-%E8%BF%90%E7%AE%97%E7%AC%A6/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/11-%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5%EF%BC%9A%E9%80%89%E6%8B%A9%E7%BB%93%E6%9E%84%EF%BC%88if%E5%92%8Cswitch%EF%BC%89/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">11-流程控制语句：选择结构（if和switch） </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/13-%E5%AF%B9%E8%B1%A1%E7%AE%80%E4%BB%8B/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">13-对象简介 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E5%AE%9A%E4%B9%89%E5%92%8C%E5%88%86%E7%B1%BB"><span class="toc-text">运算符的定义和分类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-text">运算符的定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E5%88%86%E7%B1%BB"><span class="toc-text">运算符的分类</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%97%E6%95%B0%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">算数运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%97%E6%95%B0%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E8%BF%90%E7%AE%97%E8%A7%84%E5%88%99"><span class="toc-text">算数运算符的运算规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%96%E6%A8%A1%EF%BC%88%E5%8F%96%E4%BD%99%EF%BC%89%E8%BF%90%E7%AE%97"><span class="toc-text">取模（取余）运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%AE%E7%82%B9%E6%95%B0%E8%BF%90%E7%AE%97%E7%9A%84%E7%B2%BE%E5%BA%A6%E9%97%AE%E9%A2%98"><span class="toc-text">浮点数运算的精度问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E2%80%94%E2%80%94%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E5%8A%A0%E5%8F%B7"><span class="toc-text">隐式类型转换——运算符：加号 +</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E2%80%94%E2%80%94%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A-%E3%80%81-%E3%80%81-%E3%80%81"><span class="toc-text">隐式类型转换——运算符：-、*、&#x2F;、%</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%A2%9E%E5%92%8C%E8%87%AA%E5%87%8F%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">自增和自减运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%A2%9E%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">自增运算符 ++</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%87%8F%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">自减运算符 --</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E4%B8%BE%E4%BE%8B"><span class="toc-text">代码举例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">隐式类型转换</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">一元运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#typeof"><span class="toc-text">typeof</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AD%A3%E5%8F%B7-x2F-%E8%B4%9F%E5%8F%B7%EF%BC%9A-a%E3%80%81-a"><span class="toc-text">正号&#x2F;负号：+a、-a</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E2%80%94%E2%80%94%E6%AD%A3%E5%8F%B7-x2F-%E8%B4%9F%E5%8F%B7"><span class="toc-text">隐式类型转换——正号&#x2F;负号</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E7%9B%AE%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">三目运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">逻辑运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E5%B8%83%E5%B0%94%E5%80%BC%E7%9A%84%E4%B8%8E%E6%88%96%E8%BF%90%E7%AE%97%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">非布尔值的与或运算【重要】</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E5%B8%83%E5%B0%94%E5%80%BC%E7%9A%84-%E8%BF%90%E7%AE%97"><span class="toc-text">非布尔值的 ! 运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9F%AD%E8%B7%AF%E8%BF%90%E7%AE%97%E7%9A%84%E5%A6%99%E7%94%A8%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">短路运算的妙用【重要】</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">赋值运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6%E5%8C%85%E6%8B%AC%E5%93%AA%E4%BA%9B"><span class="toc-text">赋值运算符包括哪些</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-text">注意事项</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">比较运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E6%95%B0%E5%80%BC%E7%9A%84%E6%AF%94%E8%BE%83"><span class="toc-text">非数值的比较</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AC%A6%E5%8F%B7%E7%9A%84%E5%BC%BA%E8%B0%83"><span class="toc-text">&#x3D;&#x3D;符号的强调</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%A8%E7%AD%89%E7%AC%A6%E5%8F%B7%E7%9A%84%E5%BC%BA%E8%B0%83"><span class="toc-text">&#x3D;&#x3D;&#x3D;全等符号的强调</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8D%E5%90%8C%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E4%B9%8B%E9%97%B4%E7%9A%84%E5%A4%A7%E5%B0%8F%E6%AF%94%E8%BE%83"><span class="toc-text">不同数据类型之间的大小比较</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E5%80%BC%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%85%B6%E4%BB%96%E7%B1%BB%E5%9E%8B%E6%AF%94%E8%BE%83"><span class="toc-text">数值类型和其他类型比较</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E5%A4%A7%E5%B0%8F%E6%AF%94%E8%BE%83"><span class="toc-text">日期大小比较</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%97%E5%8F%B7%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">逗号运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E4%BC%98%E5%85%88%E7%BA%A7"><span class="toc-text">运算符的优先级</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Unicode-%E7%BC%96%E7%A0%81"><span class="toc-text">Unicode 编码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E5%AE%9A%E4%B9%89%E5%92%8C%E5%88%86%E7%B1%BB"><span class="toc-text">运算符的定义和分类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-text">运算符的定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E5%88%86%E7%B1%BB"><span class="toc-text">运算符的分类</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%97%E6%95%B0%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">算数运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%97%E6%95%B0%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E8%BF%90%E7%AE%97%E8%A7%84%E5%88%99"><span class="toc-text">算数运算符的运算规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%96%E6%A8%A1%EF%BC%88%E5%8F%96%E4%BD%99%EF%BC%89%E8%BF%90%E7%AE%97"><span class="toc-text">取模（取余）运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%AE%E7%82%B9%E6%95%B0%E8%BF%90%E7%AE%97%E7%9A%84%E7%B2%BE%E5%BA%A6%E9%97%AE%E9%A2%98"><span class="toc-text">浮点数运算的精度问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E2%80%94%E2%80%94%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E5%8A%A0%E5%8F%B7"><span class="toc-text">隐式类型转换——运算符：加号 +</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E2%80%94%E2%80%94%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A-%E3%80%81-%E3%80%81-%E3%80%81"><span class="toc-text">隐式类型转换——运算符：-、*、&#x2F;、%</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%A2%9E%E5%92%8C%E8%87%AA%E5%87%8F%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">自增和自减运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%A2%9E%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">自增运算符 ++</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%87%8F%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">自减运算符 --</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E4%B8%BE%E4%BE%8B"><span class="toc-text">代码举例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">隐式类型转换</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">一元运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#typeof"><span class="toc-text">typeof</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AD%A3%E5%8F%B7-x2F-%E8%B4%9F%E5%8F%B7%EF%BC%9A-a%E3%80%81-a"><span class="toc-text">正号&#x2F;负号：+a、-a</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E2%80%94%E2%80%94%E6%AD%A3%E5%8F%B7-x2F-%E8%B4%9F%E5%8F%B7"><span class="toc-text">隐式类型转换——正号&#x2F;负号</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E7%9B%AE%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">三目运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">逻辑运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E5%B8%83%E5%B0%94%E5%80%BC%E7%9A%84%E4%B8%8E%E6%88%96%E8%BF%90%E7%AE%97%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">非布尔值的与或运算【重要】</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E5%B8%83%E5%B0%94%E5%80%BC%E7%9A%84-%E8%BF%90%E7%AE%97"><span class="toc-text">非布尔值的 ! 运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9F%AD%E8%B7%AF%E8%BF%90%E7%AE%97%E7%9A%84%E5%A6%99%E7%94%A8%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">短路运算的妙用【重要】</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">赋值运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6%E5%8C%85%E6%8B%AC%E5%93%AA%E4%BA%9B"><span class="toc-text">赋值运算符包括哪些</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-text">注意事项</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">比较运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E6%95%B0%E5%80%BC%E7%9A%84%E6%AF%94%E8%BE%83"><span class="toc-text">非数值的比较</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AC%A6%E5%8F%B7%E7%9A%84%E5%BC%BA%E8%B0%83"><span class="toc-text">&#x3D;&#x3D;符号的强调</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%A8%E7%AD%89%E7%AC%A6%E5%8F%B7%E7%9A%84%E5%BC%BA%E8%B0%83"><span class="toc-text">&#x3D;&#x3D;&#x3D;全等符号的强调</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8D%E5%90%8C%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E4%B9%8B%E9%97%B4%E7%9A%84%E5%A4%A7%E5%B0%8F%E6%AF%94%E8%BE%83"><span class="toc-text">不同数据类型之间的大小比较</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E5%80%BC%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%85%B6%E4%BB%96%E7%B1%BB%E5%9E%8B%E6%AF%94%E8%BE%83"><span class="toc-text">数值类型和其他类型比较</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E5%A4%A7%E5%B0%8F%E6%AF%94%E8%BE%83"><span class="toc-text">日期大小比较</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%97%E5%8F%B7%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">逗号运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E4%BC%98%E5%85%88%E7%BA%A7"><span class="toc-text">运算符的优先级</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Unicode-%E7%BC%96%E7%A0%81"><span class="toc-text">Unicode 编码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
